﻿using System;
using System.Linq.Expressions;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace udpEchoClient
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            int port = 7;
            IPHostEntry hostInfo = Dns.GetHostEntry("192.168.1.2");

            string message = "message";
            byte[] messageBuffer = Encoding.ASCII.GetBytes(message);

            var udpClient = new UdpClient();

            try
            {
                udpClient.Send(messageBuffer, messageBuffer.Length, hostInfo.HostName, port);

                Console.WriteLine("Sent {0} bytes to the server", messageBuffer.Length);

                var endPoint = new IPEndPoint(IPAddress.Any, 0);
                
                Thread.Sleep(300);
                byte[] rcvPacket = udpClient.Receive(ref endPoint);

                Console.WriteLine("Received {0} bytes from {1}: {2}",
                    rcvPacket.Length, endPoint,
                    Encoding.ASCII.GetString(rcvPacket, 0, rcvPacket.Length));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            udpClient.Close();
        }

    }
}